package com.cloudsigns.factorymanager2.project.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.engineering.bean.EngineeringConstructContentBean;
import com.cloudsigns.factorymanager2.project.activity.AddEngineeringConstructOrderActivity;
import com.cloudsigns.factorymanager2.project.myinerface.ItemRemoveListener;

import java.util.ArrayList;
import java.util.List;

public class ProductContentAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<EngineeringConstructContentBean> datas = new ArrayList<EngineeringConstructContentBean>();
    private LayoutInflater inflater;
    private String TAG = "";
    private ItemRemoveListener itemRemoveListener;

    public ProductContentAdapter(Context context, String TAG) {
        this.context = context;
        this.inflater = LayoutInflater.from(context);
        this.TAG = TAG;
    }

    public void setData(List<EngineeringConstructContentBean> list) {
        this.datas.clear();
        if (null != list) {
            this.datas.addAll(list);
        }

    }

    public ArrayList<EngineeringConstructContentBean> getData() {
        return datas;
    }

    public void setRemoveListener(ItemRemoveListener itemRemoveListener) {
        this.itemRemoveListener = itemRemoveListener;
    }

    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public EngineeringConstructContentBean getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup viewGroup) {
//        final ViewHolder holder;
//        if (convertView == null) {
//            holder = new ViewHolder();
//
//            convertView = inflater.inflate(R.layout.item_product_content, null);
//
//
//            convertView.setTag(holder);
//        } else {
//            holder = (ViewHolder) convertView.getTag();
//        }
        ViewHolder holder;
        if (null == convertView){

            convertView = inflater.inflate(R.layout.item_product_content,null);
            holder =new ViewHolder(convertView,position);
//            holder.edt_product_content = convertView.findViewById(R.id.edt_product_content);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        EngineeringConstructContentBean eConstructContentBean = datas.get(position);
        if (null != eConstructContentBean) {
            holder.edt_product_content.setText(eConstructContentBean.getConstructPart());
        }

        holder.img_remove.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (itemRemoveListener != null) {
                    itemRemoveListener.remove(position);
                }
            }
        });
        return convertView;
    }

//    private class ViewHolder {
//        private ImageView img_remove;
//        private EditText edt_product_content;
//    }

    class ViewHolder{
        private ImageView img_remove;
        private EditText edt_product_content;

        public ViewHolder(View view,int position){

            img_remove = view.findViewById(R.id.img_remove);
            edt_product_content = view.findViewById(R.id.edt_product_content);
            edt_product_content.setTag(position);//存tag值
//            edt_product_content.addTextChangedListener(new TextSwitcher(this));
        }
    }

    class TextSwitcher implements TextWatcher {
        private ViewHolder mHolder;

        public TextSwitcher(ViewHolder mHolder) {
            this.mHolder = mHolder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int position = (int) mHolder.edt_product_content.getTag();//取tag值
             ((AddEngineeringConstructOrderActivity)context).saveEditData(position, s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
